using MediatR;
using Microsoft.Extensions.Logging;
using NotifyCenter.MediatR.Etos;
using NotifyCenter.MediatR.Webhooks;

namespace NotifyCenter.MediatR.Handlers;

public class NotificationWebhookHandler(
    INotificationWebhookQueue queue,
    ILogger<NotificationWebhookHandler> logger) : INotificationHandler<NotificationWebhookEto>
{
    public async Task Handle(NotificationWebhookEto notification, CancellationToken cancellationToken)
    {
        ArgumentNullException.ThrowIfNull(notification);

        try
        {
            await queue.EnqueueAsync(notification, cancellationToken);
        }
        catch (Exception ex)
        {
            logger.LogError(ex, "Webhook 入队失败，记录 {RecordId}", notification.RecordId);
        }
    }
}
